package com.itheima.web;

import com.itheima.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 张义
 * @describe:
 * @date 2021/06/21
 */

public class UserServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 该上下文获取方式不是最佳的, 因为 web 会有很多 Servlet 类， 每次 doGet 都会加载一次 配置文件 , 影响性能 , 因此 之后都会采用 监听器 来实现，一旦创建就存入最大的域 servletContext 域中 , 用的时候直接拿即可
        // ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        // UserService userService = app.getBean(UserService.class);

        ServletContext servletContext = req.getServletContext();
        ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        UserService userService = app.getBean(UserService.class);
        userService.save();
    }

}
